package com.example.fragment;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.Adapter.ChannelAdapter;
import com.example.Adapter.CouponAdapter;
import com.example.Adapter.GrouponAdapter;
import com.example.api.ApiService;
import com.example.interfaces.ResponseCallback;
import com.example.shopapp.R;
import com.example.viewmodel.Banner;
import com.example.viewmodel.Channel;
import com.example.viewmodel.Coupon;
import com.example.viewmodel.Groupon;
import com.example.viewmodel.HomeData;
import com.example.viewmodel.ResponseData;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.Collections;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private com.youth.banner.Banner viewBanner;//获取Banner组件
    private GridView gv_channel;
    private RecyclerView rc_coupon;
    private RecyclerView groupon_view;
    List<Banner> bannerData;
    List<Channel> channel;
    List<Coupon> coupons;
    List<Groupon> groupons;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    public void initView(View view){
        viewBanner = view.findViewById(R.id.banner);
        gv_channel = view.findViewById(R.id.gv_channel);
        rc_coupon = view.findViewById(R.id.coupon_view);
        groupon_view = view.findViewById(R.id.groupon_view);
    }
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);//获取layout中的fragment_home.xml
        initView(view);
        ApiService.GetHome(new ResponseCallback<ResponseData<HomeData>>() {
            @Override
            public void onSuccess(ResponseData<HomeData> callback) {
                bannerData = callback.getData().getBanner();//拿到数据
                channel = callback.getData().getChannel();
                coupons = callback.getData().getCouponList();
                groupons = callback.getData().getGrouponList();

                viewBanner.setAdapter(new BannerImageAdapter<Banner>(bannerData) {
                            @Override
                            public void onBindView(BannerImageHolder holder, Banner data, int position, int size) {
                                //图片加载自己实现
                                Glide.with(holder.itemView)
                                        .load(data.getUrl())
                                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                        .into(holder.imageView);
                            }
                        })
                        .addBannerLifecycleObserver(HomeFragment.this)//添加生命周期观察者
                        .setIndicator(new CircleIndicator(view.getContext()));

                gv_channel.setAdapter(new ChannelAdapter(view.getContext(), channel));

                rc_coupon.setAdapter(new CouponAdapter(view.getContext(), coupons));
                rc_coupon.setLayoutManager(new GridLayoutManager(view.getContext(), 1));

                groupon_view.setAdapter(new GrouponAdapter(view.getContext(), groupons));
                groupon_view.setLayoutManager(new GridLayoutManager(view.getContext(), 1));
            }
        });
        return view;
    }
}